//
//  mmjClearCacheCell.m
//  Social
//
//  Created by seirra on 2018/5/20.
//  Copyright © 2018年 mmj. All rights reserved.
//

#import "mmjClearCacheCell.h"

#define mmjdefaultCachFile [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"Custom"]

@implementation mmjClearCacheCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        //[NSThread sleepForTimeInterval:5];
        
        //设置cell右边的指示器，用来说明正在处理一些事情
        UIActivityIndicatorView * loadingview = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [loadingview startAnimating];
        self.accessoryView = loadingview;
        
        //设置cell的默认文字
        self.textLabel.text = @"清除缓存（正在计算大小...）";
        
        //点击事件(如果设置文字之前设置为NO，那么文字会自动成为浅灰色)
        self.userInteractionEnabled = NO;
        
        //在子线程计算大小
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            //获得缓存路径
//            NSString * cachepath =  [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"default"];
//            mmjLog(@"%@",cachepath);
//            NSString* dirpath = [cachepath stringByAppendingPathComponent:@"default"]; //拼接路径
            unsigned long long size = mmjdefaultCachFile.fileSize;
            size += [SDImageCache sharedImageCache].getSize;
            NSString *sizetext = nil;
            //法一
//            if (size >= 1000 * 1000 * 1000) {//size>1GB
//                sizetext = [NSString stringWithFormat:@"%.1fGB",size / 1000.0 / 1000.0 / 1000.0];
//            }
//            else if (size >= 1000 * 1000) { //1MB< size <1GB
//                sizetext = [NSString stringWithFormat:@"%.1fMB",size / 1000.0 / 1000.0];
//            }
//            else if(size >= 1000){ // 1kb <size <1mb
//                sizetext = [NSString stringWithFormat:@"%.1fKB",size / 1000.0];
//            }else{
//                sizetext = [NSString stringWithFormat:@"%zdB",size];
//            }
            //法二
            if (size >= pow(10, 9)) {//size>1GB
                sizetext = [NSString stringWithFormat:@"%.1fGB",size / pow(10, 9)];
            }
            else if (size >= pow(10, 6)) { //1MB< size <1GB
                sizetext = [NSString stringWithFormat:@"%.1fMB",size / pow(10, 6)];
            }
            else if(size >= pow(10, 3)){ // 1kb <size <1mb
                sizetext = [NSString stringWithFormat:@"%.1fKB",size / pow(10, 3)];
            }else{
                sizetext = [NSString stringWithFormat:@"%zdB",size];
            }

            NSString * text = [NSString stringWithFormat:@"清除缓存(%@)",sizetext];
            //回到主线程
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置文字
                self.textLabel.text = text;
                //设置cell右边的图标
                self.accessoryView = nil;
                self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                
                //添加手势监听器
                 [self addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clearCache)]];
                
                //恢复点击事件
                self.userInteractionEnabled = YES;
            });
        });
    }
    return self;
}
//清除缓存
-(void)clearCache{
    //弹出指示器
    [SVProgressHUD showWithStatus:@"正在清楚缓存"  maskType:SVProgressHUDMaskTypeBlack];
    
    //删除缓存文件
    [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSFileManager * manager = [NSFileManager defaultManager];
            [manager removeItemAtPath:mmjdefaultCachFile error:nil];
            [manager createDirectoryAtPath:mmjdefaultCachFile withIntermediateDirectories:YES attributes:nil error:nil];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
                
                self.textLabel.text = @"清除缓存(0B)";
            });
        });
       
    }];
    
    
}
@end
